﻿/// <reference path="jquery-1.6.4-vsdoc.js" />


(function ($) {

    $.homeTilesImplementation = {

        timeBetweenFlips: 3000,

        lengthOfFlipClose: 120,

        lengthOfFlipOpen: 180,

        tileNames: ["Tile_TopArticle",
                     "Tile_Top2ndArticle",
                     "Tile_Top3rdOr4thArticle",
                     "Tile_1stTwitPic",
                     "Tile_2ndTwitPic",
                     "Tile_3rdTwitPic",
                     "Tile_4thTwitPic",
                     "Tile_1stOr2ndCategory",
                     "Tile_3rdOr4thCategory",
                     "Tile_QuadTwitPic"],

        tileGroups: [
                        ["Tile_1stOr2ndCategory", "Tile_3rdOr4thCategory"],
                        ["Tile_TopArticle"],
                        ["Tile_Top2ndArticle"],
                        ["Tile_Top3rdOr4thArticle", "Tile_Tweet"],
                        ["Tile_1stTwitPic", "Tile_2ndTwitPic", "Tile_QuadTwitPic"],
                    ],
        nextTileGroupIndex: 0,
        isDocumentActive: true,

        init: function () {

            for (tileIndex in $.homeTilesImplementation.tileNames) {
                $('.' + $.homeTilesImplementation.tileNames[tileIndex]).quickFlip({
                    vertical: true,
                    noResize: false,
                    refresh: false,
                    closeSpeed: $.homeTilesImplementation.lengthOfFlipClose,
                    openSpeed: $.homeTilesImplementation.lengthOfFlipOpen
                });
            }

            $("body").bind("onpagehide", function () {
                $.homeTilesImplementation.isDocumentActive = false;
            });

            $("body").focus(function () {
                $.homeTilesImplementation.isDocumentActive = true;
            });

            $.homeTilesImplementation.startAutoFlip();
        },

        startAutoFlip: function () {
            setTimeout(function () {

                if (!$.sideBar.get_isOpen()) {
                    var currentGroup = $.homeTilesImplementation.tileGroups[$.homeTilesImplementation.nextTileGroupIndex];
                    for (tileIndex in currentGroup) {
                        $('.' + currentGroup[tileIndex]).quickFlipper({ refresh: true });
                    }

                    $.homeTilesImplementation.nextTileGroupIndex = $.homeTilesImplementation.nextTileGroupIndex + 1;
                    if ($.homeTilesImplementation.nextTileGroupIndex == $.homeTilesImplementation.tileGroups.length) {
                        $.homeTilesImplementation.nextTileGroupIndex = 0;
                    }
                }

                $.homeTilesImplementation.startAutoFlip();
            },
            $.homeTilesImplementation.timeBetweenFlips);
        }
    };

    $.homeTiles = function () {
        new $.homeTilesImplementation.init();
    };
})(jQuery);